package org.pointex.controller;
import javax.servlet.http.HttpServletRequest;

import org.pointex.common.controller.BaseController;
import org.pointex.common.model.ParamObject;
import org.pointex.common.util.MD5Util;
import org.pointex.util.RSAUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/View/")
public class MainController extends BaseController{
	
	//鐧诲綍鎴愬姛璺宠浆鍒板晢鍩庨椤�
	/**
	 * code{0 澶辫触 1鎴愬姛 -1 鍙傛暟涓嶈兘涓虹┖ -2 楠岀澶辫触 -3 杩炴帴瓒呮椂  }
	 * @param paramObject
	 * @param model
	 * @return
	 */
	@RequestMapping("index")
	public String index(ParamObject paramObject,Model model,HttpServletRequest request){
//		String path="error";
//		Integer code=0;
//		try {
//			if(paramObject!=null){
//					if (isNuOrEmpty(paramObject.getUid(), paramObject.getName(),paramObject.getSign())) {
//						code=-1;
//					}else{
//						
//							RSAUtils.decryptPtion(paramObject);
//							//鐧诲綍楠岃瘉绛惧悕
//							if(MD5Util.isSign(paramObject)){
//								path="index";
//								code=1;
//							}else{
//								code=-2; 
//							}
//						
//					}
//				
//			}else{
//				code=-1;
//			}
//		} catch (Exception e) {
//			logger.info("璺宠浆鍟嗗煄棣栭〉寮傚父", e.getMessage());
//			e.printStackTrace();
//			throw new RuntimeException("璺宠浆鍟嗗煄棣栭〉寮傚父", e);
//		} finally {
//			try {
//				model.addAttribute("code", code);
//			} catch (Exception e) {
//				code=-3;
//				model.addAttribute("code", code);
//				throw new RuntimeException("缃戠粶绻佸繖锛岃绋嶅悗鍐嶈瘯", e);
//			}
//		}
		return "View/index";
	}
	
	/*
	public Integer getPublicInteface(ParamObject paramObject,String url){
		paramObject.setSign( MD5Util.MD5Sign(paramObject.getUid() + paramObject.getName() + "sign", MD5Util.MD5KEY));
		//RSAUtils.isSign(uid, name, sign);
		String param="uid="+paramObject.getUid()+"&name="+paramObject.getName()+"&sign="+paramObject.getSign();
		String JsonString=HttpRequest.sendPost(url,param);
		JSONObject jsonObject= JSON.parseObject(JsonString);
		Integer code=0;
		if(jsonObject!=null){
			if(jsonObject.get("code")!=null){
				code=(Integer) jsonObject.get("code");
				if(code==1){
					String public_expont=jsonObject.get("uid").toString();//鍏挜鎸囨暟
					String public_model=jsonObject.get("name").toString();//妯�
					RSAPublicKey publicKey=RSAUtils.getPublicKey(public_model, public_expont);
					try {
						paramObject.setUid(RSAUtils.encryptByPublicKey(paramObject.getUid(), publicKey));
						paramObject.setName(RSAUtils.encryptByPublicKey(paramObject.getName(), publicKey));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
					System.out.println("澶辫触");
				}
			}
		}
		return code;
	}*/
	
	@RequestMapping("login")
	public String redLogin(HttpServletRequest request,ParamObject obj){
		request.setAttribute("uid", obj.getUid());
		request.setAttribute("name", obj.getName());
		request.setAttribute("sign", obj.getSign());
		String flag=getSession().getAttribute("isLogin")+"";
		getSession().setMaxInactiveInterval(600);
		if(flag.equals("true")){
			return "View/index";
		}
		return "View/login";
	}
	
	@RequestMapping("authLogin")
	public String login(HttpServletRequest request,ParamObject obj){
		request.setAttribute("uid", obj.getUid());
		request.setAttribute("name", obj.getName());
		request.setAttribute("sign", obj.getSign());
		return "View/login";
	}
	
	@RequestMapping("error")
	public String error(HttpServletRequest request,ParamObject obj){
		return "View/error";
	}
	
	@RequestMapping("refuse")
	public String refuse(HttpServletRequest request,ParamObject obj){
		return "View/refuse";
	}
	

	
}
